package com.flyoil.petromp.adapter.adapter_course;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.cnpc.fyviewlibrary.view.ListScrollview;
import com.flyoil.petromp.R;
import com.flyoil.petromp.base.BaseRecyAdapter;
import com.flyoil.petromp.entity.entity_course_list.CourseEntity;
import com.flyoil.petromp.utils.OrderUtl.OrderListUtil;

/**
 * Created by songzhiiyn on 2017/10/11.
 * 流程列表第一级列表的适配器
 */

public class ItemCourseGroupAdapter extends BaseRecyAdapter<ItemCourseGroupAdapter.ViewHoder, CourseEntity> {
    private OnChidrenItemClickLinester onChidrenItemClickLinester;

    public ItemCourseGroupAdapter(Context mContext) {
        super(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHoder(mInflater.inflate(R.layout.item_course_list_group_adapter, parent, false));
    }

    public void setOnChidrenItemClickLinester(OnChidrenItemClickLinester onChidrenItemClickLinester) {
        this.onChidrenItemClickLinester = onChidrenItemClickLinester;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final CourseEntity entity = getItemData(position);
        ViewHoder viewHoder = (ViewHoder) holder;
        ItemCourseChildrenAdapter itemCourseChildrenAdapter = new ItemCourseChildrenAdapter(mContext);
        viewHoder.list.setAdapter(itemCourseChildrenAdapter);
        viewHoder.tv_name.setText(entity.getTitle());
        viewHoder.img_icon.setImageResource(OrderListUtil.getImageResource(entity.getType()));
        itemCourseChildrenAdapter.addDataBottom(entity.getChildrens());
        viewHoder.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (onChidrenItemClickLinester != null) {
                    onChidrenItemClickLinester.Onclick(i, entity.getChildrens().get(i));
                }
            }
        });

    }

    class ViewHoder extends RecyclerView.ViewHolder {
        private ListScrollview list;
        private TextView tv_name;
        private ImageView img_icon;

        public ViewHoder(View itemView) {
            super(itemView);
            list = itemView.findViewById(R.id.list_item_course_list_group_childrens);
            tv_name = itemView.findViewById(R.id.tv_item_course_list_group_name);
            img_icon = itemView.findViewById(R.id.img_item_course_list_group_icon);
            list.setDividerHeight(0);
        }
    }


    public interface OnChidrenItemClickLinester {
        public void Onclick(int position, Object entity);
    }
}
